//
//  SelectCarTypeView.m
//  AutoHome
//
//  Created by wqh on 15-5-13.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "SelectCarTypeView.h"
#import "CarTypeDetail.h"

#define kSelectCarTypeViewCell @"selectCarTypeViewCell"

@implementation SelectCarTypeView

- (void)awakeFromNib {
    [self createTableView];
    //注册观察者，监听上级界面 以及时更新数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initData) name:@"updateSelectCarTypeView" object:nil];
    
}

- (void)dealloc {
    //销毁观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//点击关闭按钮
- (IBAction)closeBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.5 animations:^{
        //首先把自身移出界面
        self.frame= CGRectMake(kScreenWidth, 20, kScreenWidth-50, kScreenHeight-20);
        //在把半透明背景视图透明度改为0
        if (self.myCloseViewBlock) {
            self.myCloseViewBlock();
        }
    }];
}

#pragma mark -收到通知后会及时加载数据
- (void)initData {
    //初始化数据源
    self.dataArr = [NSMutableArray array];
    self.sectionTitleArr = [NSMutableArray array];
    
    [self updateHeaderView:self.carSystemModel];
    
    NSArray *array = self.carSystemModel.specitemgroups;
    for (NSDictionary *dict in array) {
        NSString *sectionTitle = dict[@"groupname"];
        [self.sectionTitleArr addObject:sectionTitle];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *carDict in dict[@"specitems"]) {
            CarTypeModel *model = [[CarTypeModel alloc] init];
            [model setValuesForKeysWithDictionary:carDict];
            [array addObject:model];
        }
        [self.dataArr addObject:array];
    }
    [self.tableView reloadData];
    
}

#pragma mark - 更新tableView上面两个视图的内容
- (void)updateHeaderView:(CarSystemModel *)model {
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"default_80_60"]];
    self.seirseNameLabel.text = model.seriesname;
    self.priceLabel.text = model.price;
}

#pragma mark - 创建tableView
- (void)createTableView {
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 124, kScreenWidth-40, kScreenHeight-124) style:UITableViewStylePlain];
    self.tableView.frame = CGRectMake(0, 124, kScreenWidth-40, kScreenHeight-124-20);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSelectCarTypeViewCell];

}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [(NSMutableArray *)_dataArr[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSelectCarTypeViewCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CarTypeModel *model = self.dataArr[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont italicSystemFontOfSize:14];
    cell.textLabel.text = model.name;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,20)];
    label.backgroundColor = [UIColor customGrayColor];
    label.text = [NSString stringWithFormat:@"    %@",self.sectionTitleArr[section]];
    
    label.font = [UIFont italicSystemFontOfSize:12];
    return label;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CarTypeModel *model = self.dataArr[indexPath.section][indexPath.row];
    CarTypeDetail *carTypeDetail = [[CarTypeDetail alloc] init];
    
    /**
     *  把访问接口需要用到得参数传过去
     */
    carTypeDetail.systemId = self.carSystemModel.id;
    carTypeDetail.typeId = model.id ;
    carTypeDetail.titleName = model.name;
    
    if (self.myJumpPageBlock) {
        self.myJumpPageBlock(carTypeDetail);
    }
}


@end
